package com.yanqu.road.dao.impl.union.activitygroup;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.union.activitygroup.UnionActivityGroupRule;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class UnionActivityGroupRuleDaoImpl extends TempDao {

    private static String getUnionActivityGroupRuleSql = "select * from `t_s_union_activity_group_rule` where expireTime>? or expireTime=0";

    public List<UnionActivityGroupRule> getUnionActivityGroupRuleList() {
        List<UnionActivityGroupRule> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(getUnionActivityGroupRuleSql);
            int i = 1;
            call.setLong(i++, System.currentTimeMillis());
            rs = call.executeQuery();
            while (rs.next()) {
                UnionActivityGroupRule unionActivityGroupRule = new UnionActivityGroupRule();
                unionActivityGroupRule.setActivityType(rs.getInt("activityType"));
                unionActivityGroupRule.setChildType(rs.getInt("childType"));
                unionActivityGroupRule.setUnionUid1(rs.getString("unionUid1"));
                unionActivityGroupRule.setUnionUid2(rs.getString("unionUid2"));
                unionActivityGroupRule.setExpireTime(rs.getLong("expireTime"));
                list.add(unionActivityGroupRule);
            }
        } catch (Exception e) {
            logger.error(Thread.currentThread().getStackTrace()[1].getMethodName() + ":", e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

}
